/**
 * 
 */
package com.track;



import java.io.BufferedInputStream;

import android.os.Bundle;
import android.app.*;
import android.util.Log;
import android.content.*;
import android.content.res.Resources;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.Toast;
//import com.google.android.maps.*;


import com.track.views.MapTileView;

// gps
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationListener;

/**
 * @author nbukauskas
 *
 */
public class mapview extends Activity{
	
	
	// Menu set up
	public Menu mMenu;
	private static final int sMapMenuResources[] = { R.menu.map_page_menu};
	
	// mapping set up
	public final int [] zoomLevels = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17};
	public static int currentZoomLevel = 11;
	
	public static double currentMapLat = -73.90022; // this to be rigged to load from preferences saved
	public static double currentMapLon = 40.77482;
	
	
	public static double currentTargetLat = 40;
	public static double currentTargetLon = -73;
	
	public static boolean navOn = false;
	
	private LocationManager _mLocationManager;
	private LocationListener _mLocationListener;
	private MapTileView _map;
	private Boolean isNavigable = false;
	
	private Thread mMapLoadThread;
	private Runnable mMapLoadRunnable;
	
	private Thread mMapUIThread;
	private Runnable mMapUIRunnable;
	
	private Thread mMapMenuThread;
	
	private Resources res;
	private static MapTileView mView;
	private static LinearLayout container;
	public void onCreate(Bundle savedInstanceState){
		
		
		super.onCreate(savedInstanceState);
		res = getResources();
        
        container = new LinearLayout(this);
        container.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        setContentView(container);
        
       
        mView = new MapTileView(this);
        
        container.addView(mView);
        
		
		
		//spawnMainThreads();
		//setUpLocationServices();
		
		
		
	};
	
	protected final void setUpLocationServices(){
		
		//BufferedInputStream bIns;s
		
		_mLocationListener = new LocationListener(){

			public void onLocationChanged(Location location) {
				onLocationUpdate(location);
			}

			public void onProviderDisabled(String provider) {
				// TODO Auto-generated method stub
				
			}

			public void onProviderEnabled(String provider) {
				// TODO Auto-generated method stub
				
				}
	
				public void onStatusChanged(String provider, int status, Bundle extras) {
				// TODO Auto-generated method stub	
				
			}
			
		};
		
		// GPS set up - start with Network based services
		if(navOn)
		{
			_mLocationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
			_mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, _mLocationListener);
			
			//TODO: Intelligent Location Service selection - 
			//_mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, _mLocationListener);
			
		} else {
			
			handleMapContentLoading();
			Log.w("PRESET  -- location -- >>>", ("Lat: " + currentMapLat + " Lon: " + currentMapLon));
		}
			
		
		//
		
		
		
	}
	
	private final void handleMapContentLoading()
	{
		//MapLoadThread = new Thread();
		//MapLoadThread.setRunning(true);
		
		_map.drawMapTiles();
		
		
	}
	private final void onLocationUpdate(Location loc){
		
		
			Log.w("NAV MANAGER -- location -- >>>", (loc.getLatitude() + ":" + loc.getLongitude()));
		
			//TODO:rig a proper listener for this

			currentMapLat = loc.getLatitude();
			currentMapLon = loc.getLongitude();
			_map.drawMapTiles();
		
		//isNavigable = true;
	}
	
	// Bottom MENU stuff
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
      
        mMenu = menu;
        
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(sMapMenuResources[0], menu);      
        return true;
    }
	 
	 public void returnToMainSelector(){
  		 
		 Intent intent = new Intent();
		 intent.setClass(mapview.this, select.class);
		 startActivity(intent);
		 finish();
		
	 }
   
	 @Override
	    public boolean onOptionsItemSelected(MenuItem item) {
	        switch (item.getItemId()) {
	            case R.id.map_menu_back:
	            	returnToMainSelector();
	                return true;
	            /* Will implemet these later
	            case R.id.map_menu_boat:
	                Toast.makeText(this, "This will display current boat position centered!", Toast.LENGTH_SHORT).show();
	                return true;
	                
	            case R.id.map_menu_tides:     
	            	 Toast.makeText(this, "This will display tide information!", Toast.LENGTH_SHORT).show();
	                break;
	            
	            */
	            case R.id.map_menu_zoomIn:
	            	
	            	currentZoomLevel += 1;
	            	_map.drawMapTiles();
	            	
	            	return true;
	            case R.id.map_menu_zoomOut:
	            	
	            	currentZoomLevel -= 1;
	            	_map.drawMapTiles();
	            	
	            	return true;
	            default:
	            	
	                
	                break;
	        }
	        
	        return false;
	    }

	
	// UTILITIES - TO BE REMOVED LATER
	protected final void drawNavUpdateToast(){
		
		Toast.makeText(this, "Lat:" + currentMapLat + " Lon: " + currentMapLon , Toast.LENGTH_LONG).show();
	}

	@Override
	protected void onDestroy(){
		super.onDestroy();
		
		// stop any Location management on the device
		_mLocationManager.removeUpdates(_mLocationListener);
		_mLocationManager = null;
		_mLocationListener = null;
		
		// clean up the rest of it
		//TODO: Graceful clean exit from the map view
	}

}
